package utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author: dm
 * @date: 2023/1/4 9:41
 * @description:
 */
public class DateUtils {
    /**
     * 根据年月获取月末最后一天日期
     * @param year
     * @param month
     * @return
     */
    public static String getLastDay(int year, int month, String format) {
        Calendar cale = Calendar.getInstance();
        //赋值年份
        cale.set(Calendar.YEAR, year);
        //赋值月份
        cale.set(Calendar.MONTH, month - 1);
        //获取月最大天数
        int lastDay = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cale.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期yyyy-MM-dd
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(cale.getTime());
    }
}
